﻿/*******************************通用接口 公共方法 start  sndddx 20180212 *******************************/


//常用地址
window.netServerUrl = "~";

/**
* 静态常量参数类
*/
var ConstProperties = {
    url: window.netServerUrl,// 服务器地址
    printFlage: true,//是否打印
};

/**
* ajax共通工具类
*/
var AjaxTools = {
    /**
     * get提交数据
     * @param {any} url
     * @param {any} Para
     * @param {any} SuccessFunc
     */
    GetAjax: function (url, Para, SuccessFunc) {
        $.ajax({
            type: "get",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(Para),
            dataType: "json",
            url: url,
            success: function (data) {
                console && console.log("---------异步访问成功 START----------");
                console && console.log("------------GetAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("返回的数据：");
                console && console.log(data);
                console && console.log("---------异步访问成功 END----------");
                SuccessFunc(data);
            },
            error: function (xhr, type, errorThrown) {
                console && console.log("---------异步访问失败 START----------");
                console && console.log("------------GetAjaxData-------------");
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("错误消息：");
                console && console.log(xhr);
                console && console.log(type);
                console && console.log(errorThrown);
                console && console.log("---------异步访问失败 END----------");
                console && console.log('网络不好，请稍后重试');
            }
        });
    },
    /**
     * post提交数据
     * @param {any} url
     * @param {any} Para
     * @param {any} successFunc
     */
    PostAjax: function (url, Para, successFunc) {
        $.ajax({
            type: "post",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(Para),
            dataType: "json",
            url: url,
            success: function (data) {
                console && console.log("---------异步访问成功 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("返回的数据：");
                console && console.log(data);
                console && console.log("---------异步访问成功 END----------");

                successFunc(data);
            },
            error: function (xhr, type, errorThrown) {
                console && console.log("---------异步访问失败 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("错误消息：");
                console && console.log(xhr);
                console && console.log(type);
                console && console.log(errorThrown);
                console && console.log("---------异步访问失败 END----------");
                console && console.log('网络不好，请稍后重试');
            }
        });
    },
    /**
     * Put请求方法
     * @param {any} url
     * @param {any} Para
     * @param {any} successFunc
     */
    PutAjax: function (url, Para, successFunc) {
        $.ajax({
            type: "put",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(Para),
            dataType: "json",
            url: url,
            success: function (data) {
                console && console.log("---------异步访问成功 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("返回的数据：");
                console && console.log(data);
                console && console.log("---------异步访问成功 END----------");

                successFunc(data);
            },
            error: function (xhr, type, errorThrown) {
                console && console.log("---------异步访问失败 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("错误消息：");
                console && console.log(xhr);
                console && console.log(type);
                console && console.log(errorThrown);
                console && console.log("---------异步访问失败 END----------");
                console && console.log('网络不好，请稍后重试');
            }
        });
    },
    /**
     * Delete请求方法
     * @param {any} url
     * @param {any} Para
     * @param {any} successFunc
     */
    DeleteAjax: function (url, Para, successFunc) {
        $.ajax({
            type: "DELETE",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(Para),
            dataType: "json",
            url: url,
            success: function (data) {
                console && console.log("---------异步访问成功 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("返回的数据：");
                console && console.log(data);
                console && console.log("---------异步访问成功 END----------");

                successFunc(data);
            },
            error: function (xhr, type, errorThrown) {
                console && console.log("---------异步访问失败 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("错误消息：");
                console && console.log(xhr);
                console && console.log(type);
                console && console.log(errorThrown);
                console && console.log("---------异步访问失败 END----------");
                console && console.log('网络不好，请稍后重试');
            }
        });
    },
    /**
     * post提交form数据
     * @param {any} url 路径
     * @param {any} formPara form的ID
     * @param {any} successFunc 回调函数
     */
    PostAjaxFormData: function (url, formPara, successFunc) {
        $.ajax({
            type: "post",
            contentType: "application/json; charset=utf-8",
            data: $('#' + formPara).serialize(),
            dataType: "json",
            url: url,
            success: function (data) {
                console && console.log("---------异步访问成功 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("返回的数据：");
                console && console.log(data);
                console && console.log("---------异步访问成功 END----------");

                successFunc(data);
            },
            error: function (xhr, type, errorThrown) {
                console && console.log("---------异步访问失败 START----------");
                console && console.log("------------PostAjaxData-------------");
                console && console.log("访问路径：" + url);
                console && console.log("参数：");
                console && console.log(Para);
                console && console.log("错误消息：");
                console && console.log(xhr);
                console && console.log(type);
                console && console.log(errorThrown);
                console && console.log("---------异步访问失败 END----------");
                console && console.log('网络不好，请稍后重试');
            }
        });
    },
};


/**
*  通用方法类
*/
var ToolsHelper = {
    //判断数据是否为空
    IsNull: function (val) {
        if (val == "" || val == null || typeof val == "undefined") {
            return true;
        }
        return false;
    },
    //获取路径下参数值
    GetQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var d = decodeURI(window.location.search)
        var r = d.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    },
    ClearAllLocalSTorage: function () {
        localStorage.clear();
    },
    InitLocalStorage: function (arr) {//初始化本地数据
        var Dic = [];
        for (var index = 0; index < arr.length; index++) {
            var key = arr[index];
            var val = localStorage.getItem(key);
            var Item = {};
            Item["Key"] = key;
            Item["Val"] = IsNull(val) ? "" : val;
            Dic.push(Item);
        };
        localStorage.clear();
        for (var index = 0; index < Dic.length; index++) {
            var Item = Dic[index];
            localStorage.setItem(Item.Key, Item.Val);
        }
    },
    SetLocalStorage: function (key, value) {//存本地数据
        localStorage.setItem(key, value);
    },
    GetLocalStorage: function (key) {//根据key值取本地数据
        var value = localStorage.getItem(key);
        if (mToolsHelper.IsNull(value)) {
            value = "";
        }
        return value;
    },
    GetFullDateStr: function (month, splitChar, format) {//获取日期的格式化数据
        if (!mToolsHelper.IsNull(month)) {
            var today = new Date();
            var year = today.getFullYear().toString();
            var regdateStr = new Date(Date.parse((year.toString() + '/' + month.toString()).replace(/-/g, "/"))).format(format);
            return regdateStr;
        } else {
            return "";
        }
    },
    print: function (content, type) {
        if (ConstProperties.printFlage || type) {
            console.log(content);
        }
    },
};


/*******************************通用接口 公共方法 *******************************/
Date.prototype.format = function (format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}
Date.daysInMonth = function (year, month) {
    if (month == 1) {
        if (year % 4 == 0 && year % 100 != 0)
            return 29;
        else
            return 28;
    } else if ((month <= 6 && month % 2 == 0) || (month = 6 && month % 2 == 1))
        return 31;
    else
        return 30;
};
Date.prototype.addMonth = function (addMonth) {
    var y = this.getFullYear();
    var m = this.getMonth();
    var nextY = y;
    var nextM = m;
    //如果当前月+要加上的月>11 这里之所以用11是因为 js的月份从0开始
    if (m > 11) {
        nextY = y + 1;
        nextM = parseInt(m + addMonth) - 11;
    } else {
        nextM = this.getMonth() + addMonth
    }
    var daysInNextMonth = Date.daysInMonth(nextY, nextM);
    var day = this.getDate();
    if (day > daysInNextMonth) {
        day = daysInNextMonth;
    }
    return new Date(nextY, nextM, day);
};
Date.prototype.addDays = function (addDays) {
    var d = new Date();
    d.setDate(d.getDate() + addDays);
    var m = d.getMonth();//+ 1;
    //if (isIOS) {
    //    m += 1;
    //}
    return new Date(d.getFullYear(), m, d.getDate());
};
Date.prototype.addUserDays = function (userDate, addDays) {
    var d = new Date(userDate.getFullYear(), userDate.getMonth(), userDate.getDate());
    d.setDate(d.getDate() + addDays);
    var m = d.getMonth();//+ 1;
    //if (isIOS) {
    //    m += 1;
    //}
    return new Date(d.getFullYear(), m, d.getDate());
};
